﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using Autofac;
using Newtonsoft.Json.Linq;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using Pay.Core;
using Pay.DTO.card;
using static Pay.DTO.card.bank_carddetail_resp;

namespace Pay.Api.Controllers {
	/// <summary>
	/// 接口
	/// </summary>
	[RoutePrefix("")]
	public partial class OpenController : BaseController {
		private IComponentContext _iocContext;
		private ISettingService _iSettingService;
		private IChargeService _iChargeService;
		private IMerchantService _iMerchantService;
		private IRefundService _iRefundService;
		private IBankNumberService _iBankNumberService;
		private ITransferService _iTransferService;
		private IBillService _iBillService;
		private ICategoryService _iCategoryService;
		private IPayHtmlService _iPayHtmlService;
		private IPayNotifyService _iPayNotifyService;
		private ISkuInfoService _iSkuInfoService;
		private IProductService _iProductService;
		private IOrderService _iOrderService;
		private ITopicService _iTopicService;
        private ICardService _iCardService;
		private IGoodDeclarationService _iGoodDeclarationService;
        private IManageService _iManageService;
        private IDistributionProductService _iDistributionProductService;
        private ILogisticsDetailsService _iLogisticsDetailsService;
        private ITokenUserService _iTokenUserService;
        private IHtlMallService _iHtlMallService;
        private IHniaoService _iHniaoService;
        private ITokenBillService _iTokenBillService;
        private IRegionService _regionService;
        protected static readonly object jwuctLock = new object();
		protected static readonly object hf_nativeLock = new object();
		protected static readonly object hf_gatewayLock = new object();

		public OpenController(IComponentContext iocContext, ISettingService iSettingService,
							  IChargeService iChargeService, IMerchantService iMerchantService,
							  IRefundService iRefundService, IBankNumberService iBankNumberService,
							  ITransferService iTransferService, IBillService iBillService,
							  ICategoryService iCategoryService,IHtlMallService iHtlMallService,
						      IPayHtmlService iPayHtmlService, IPayNotifyService iPayNotifyService,
							  ISkuInfoService iSkuInfoService, IProductService iProductService,
							  IOrderService iOrderService, ITopicService iTopicService,
		                      ICardService iCardService,IGoodDeclarationService iGoodDeclarationService,
                              IManageService iManageService , IDistributionProductService  iDistributionProductService, 
                              ILogisticsDetailsService iLogisticsDetailsService,ITokenBillService iTokenBillService,
                              ITokenUserService iTokenUserService,IHniaoService iHniaoService,IRegionService regionService) {
			_iocContext = iocContext;
			_iSettingService = iSettingService;
			_iChargeService = iChargeService;
			_iMerchantService = iMerchantService;
			_iRefundService = iRefundService;
			_iBankNumberService = iBankNumberService;
			_iTransferService = iTransferService;
			_iBillService = iBillService;
			_iCategoryService = iCategoryService;
			_iPayHtmlService = iPayHtmlService;
			_iPayNotifyService = iPayNotifyService;
			_iProductService = iProductService;
			_iSkuInfoService = iSkuInfoService;
			_iOrderService = iOrderService;
			_iTopicService = iTopicService;
            _iCardService = iCardService;
			_iGoodDeclarationService = iGoodDeclarationService;
            _iManageService = iManageService;
            _iDistributionProductService = iDistributionProductService;
            _iLogisticsDetailsService = iLogisticsDetailsService;
            _iTokenUserService = iTokenUserService;
            _iHtlMallService = iHtlMallService;
            _iHniaoService = iHniaoService;
            _iTokenBillService = iTokenBillService;
            _regionService = regionService;
        }

		[Route("open"), HttpPost]
		public async Task<ApiResultMessage> OpenApi([FromBody]JObject model) {
			var log = IocHelper.Get<ILogHelper>("info");
			string method = model["method"].ToString();
			switch (method) {
				case "1000":
					return BranchBank(model.ToObject<branch_model>());
				case "1001":
					return GetOssToken(model.ToObject<head_req>());
				case "1002":
					return DeleteFile(model.ToObject<delete_file_model>());
				case "1003":
					return GetFolder(model.ToObject<folder_model>());
				case "1004":
					return await BankVaid(model.ToObject<bank_info_model>());
				//商户相关
				case "2000":
					log.Info(model.ToString());
					//单一平台调用
					return await AuthMerchant(model.ToObject<verify_auth_model>());
				case "2001":
					//外接口
					log.Info(model.ToString());
					return await AuthMerchant(model.ToObject<merchant_register_model>());
				case "2002":
					//批量
					log.Info(model.ToString());
					return await AuthMerchant(model.ToObject<batch_auth_model>());
				case "2003":
					return ApiResultToJson.JsonData(GetBill(model.ToObject<head_req>()));
				case "2004":
					return GetBillDetails(model.ToObject<bill_model>());
				case "2005":
					//扫码进件
					log.Info(model.ToString());
					return await OpenNaive(model.ToObject<native_merchant_model>());
				case "2006":
					//查询商户费率率
					return QueryFee(model.ToObject<query_fee_req>());
				case "2007":
					//实名列表
					return GetAuthInfo(model.ToObject<auth_info_query>());
				case "2008":
					//商户注册
					return CreateMerchant(model.ToObject<register_model_ex>());
				//网关
				case "3000":
					return ApiResultToJson.JsonData(GatewayPayBank(model.ToObject<gateway_bank>()));
				case "3001":
					return await GatewayPay(model.ToObject<gateway_pay_model>());
				case "3002":
					return await GatewayPayQuery(model.ToObject<refund_model_query>());
				case "3003":
					return await GatewayPayRefund(model.ToObject<refund_model>());
				case "3004":
					return await GatewayPayRefundQuery(model.ToObject<refund_model_query>());
				//扫码
				case "4000":
					log.Info(model.ToString());
					return await NativePay(model.ToObject<native_model>());
				//升级
				case "4001":
					log.Info(model.ToString());
					return await NativePayUp(model.ToObject<native_model>());
				//商城支付
				case "4002":
					log.Info(model.ToString());
					return await MallPay(model.ToObject<mall_model>());
				//快捷
				case "5000":
					log.Info(model.ToString());
					return await QuickPay(model.ToObject<quick_pay_model>());
				case "5001":
					log.Info(model.ToString());
					return await QuickPaySendsms(model.ToObject<quick_sendsms_model>());
				case "5002":
					log.Info(model.ToString());
					return await HfQuickPay(model.ToObject<quick_sms_model>());
				case "5003":
					log.Info(model.ToString());
					return await QuickPayQuery(model.ToObject<refund_model_query>());
				case "5004":
					log.Info(model.ToString());
					return await QuickPayRefund(model.ToObject<refund_model>());
				case "5005":
					log.Info(model.ToString());
					return await QuickPayRefundQuery(model.ToObject<refund_model_query>());
				case "5006":
					log.Info(model.ToString());
					return await HFQuickUpPay(model.ToObject<quick_pay_model>());
				//提现－代付
				case "6000":
					log.Info(model.ToString());
					return await Transfer(model.ToObject<apply_transfer>());
				case "6001":
					log.Info(model.ToString());
					return await TransferQuery(model.ToObject<transfer_query>());
				case "6002":
					log.Info(model.ToString());
					return OemTransferQuery(model.ToObject<transfer_ex_query>());
				//代收
				case "8000":
					return await InsteadReceive(model.ToObject<verify_model>());
				case "8001":
					return await InsteadReceive(model.ToObject<verify_model_sms>());
				case "8002":
					return await AgencyFund(model.ToObject<apply_receive>());
				//电商相关
				case "9002":
					//创建或编辑SKU属性
					return _iSkuInfoService.Save(model.ToObject<sku_info_model>());
				case "9011":
					return ApiResultToJson.JsonData(_iSkuInfoService.Gets(_iMerchantService.Get(model.ToObject<head_req>().bind_no).id));
				case "9012":
					return ApiResultToJson.JsonData(_iSkuInfoService.Get(model.ToObject<query_frist_model>().id));
				case "9013":
					//删除规格
					return _iSkuInfoService.DeleteSku(model.ToObject<query_frist_model>().id);
				case "9003":
					//创建或编辑商品
					return SaveProduct(model.ToObject<product_model>());
				case "9004":
					//商品列表数据
					log.Info(model.ToJson());
					return Products(model.ToObject<goods_query>());
				case "9005":
					//获取一条商品
					return ApiResultToJson.JsonData(_iProductService.Get(model.ToObject<query_frist_model>().id));
				case "9006":
					//购物车的商品集合请求返回最新价格、商品名称、图片、单价、市场价、卖家编号、是否可以购买
					return _iProductService.Cart(model.ToObject<cart_req_model>());
				case "9007":
					//购物车的商品集合请求验证价格和是否可以购买品
					return _iProductService.ValidProduct(model.ToObject<cart_req_model>());
				case "9008":
					//创建缓存订单，真订单要再通道异步通知生成
					return _iPayHtmlService.Save(model.ToObject<pay_html_model>());
				case "9009":
					//商户发货
					return _iOrderService.OrderDelivery(model.ToObject<order_model>());
				case "9010":
					//买家确认收货
					return _iOrderService.OrderReceipt(model.ToObject<order_model>());
				case "9014":
					//删除分类
					return _iCategoryService.DeleteCategory(model.ToObject<query_frist_model>().id);
				case "9015":
					//删除商品
					return _iProductService.DeleteProduct(model.ToObject<query_frist_model>().id);
				case "9020":
					//获取一个商品分类类
					return ApiResultToJson.JsonData(_iCategoryService.Get(model.ToObject<query_frist_model>().id));
				case "9021":
					//创建或编辑商品分类
					return SaveCategory(model.ToObject<category_model>());
				case "9022":
					//获取全部商品分类
					var head_req = model.ToObject<head_req>();
					string bind_no = string.IsNullOrEmpty(head_req.bind_no) ? head_req.merchant_no : head_req.bind_no;
					return ApiResultToJson.JsonData(_iCategoryService.GetAll(_iMerchantService.Get(bind_no).id));
				case "9023":
					//商品上架、下架
					return ApiResultToJson.JsonData(_iProductService.UpDown(model.ToObject<query_frist_model>().id));
				case "9024":
					//商品审核、违规下架
					var audit = model.ToObject<product_audit_req>();
					return ApiResultToJson.JsonData(_iProductService.Audit(audit.id, audit.audit_status, audit.audit_reason));
                case "9025":
                    //订单物流查询
                    return GetLogistic(model.ToObject<query_no_model>().order_no);
                //跨境电商专题
                case "10000":
					//获取专题列表	
					return Topics(model.ToObject<topic_query>());
				case "10001":
					//创建或编辑专题
					return SaveTopic(model.ToObject<topic_model>());
				case "10002":
					//获取一条专题
					return ApiResultToJson.JsonData(_iTopicService.GetTopic(model.ToObject<topic_model>().id));
				case "10003":
					//删除专题
					return ApiResultToJson.JsonData(_iTopicService.DeleteTopic(model.ToObject<topic_model>().id));
				case "10004":
					//获取专题列表-app
					return ApiResultToJson.JsonData(_iTopicService.GetGroup(model.ToObject<topic_query_index>()));
				case "10005":
					//获取申报成功商品
					return ApiResultToJson.Json(_iGoodDeclarationService.GetGoodDeclarations(model.ToObject<GoodDeclaQuery>()));
				case "10006":
					//校验申报商品
					return ApiResultToJson.JsonData(_iGoodDeclarationService.VerifyGoodDeclaration(model.ToObject<query_frist_model>().id));
                //网申网贷
                case "11000":
                    //获取银行列表
                    return ApiResultToJson.JsonData(await _iCardService.GetBank());
                case "11001":
                    //获取主题列表
                    return ApiResultToJson.JsonData(await _iCardService.GetBankTopic());
                case "11002":
                    //获取更多(标签)
                    return ApiResultToJson.JsonData(await _iCardService.GetBankTag());
                case "11003":
                    //获取银行通道列表
                    var list = await _iCardService.GetBankCard(model.ToObject<card_req>());
                    var result = new ApiPagedResult<bank_card_resp>(list.rows, list.total_result, list.page, list.size);
                    return ApiResultToJson.Json(result);
                case "11004":
                    //获取卡片详情
                    return ApiResultToJson.JsonData(await _iCardService.GetBankCardDetail(model.ToObject<query_frist_model>().id));
                case "11005":
                    // 保存申卡记录
                    return ApiResultToJson.JsonData(await _iCardService.Post(model.ToObject<card_save_model>()));
                case "11006":
                    // 风信子实名认证
                    return await VNoCard(model.ToObject<fengxinzi_cardno_model>());
                //拓垦用户升级支付
                case "11007":
                    log.Info(model.ToString());
                    return await TokenUserUpgradePay(model.ToObject<native_model>());
            }
            return ApiResultToJson.Json<ApiResultMessage>("请求失败");
		}
	}
}
